import { FileType, Uri } from 'vscode';
import { GlobalConst } from '../../../constants';
import { ISystemFile } from '../interface/i-system-file';
import { SystemFileStat } from './file-stat';

/**
 * 文件
 *
 * @author chitanda
 * @date 2022-12-14 10:12:29
 * @export
 * @class File
 * @extends {SystemFileStat}
 */
export class File extends SystemFileStat {
  /**
   * 文件内容
   *
   * @author chitanda
   * @date 2021-11-09 09:11:59
   * @type {Uint8Array}
   */
  get data(): Uint8Array {
    return Buffer.from(
      this.content.data.templCode || this.content.data.templcode || '',
    );
  }

  set data(val: Uint8Array) {
    this.mtime = Date.now();
    this.size = val.byteLength;
    const code = val.toString();
    this.content.data.templCode = code;
    this.content.data.templcode = code;
  }

  get fullPath(): string {
    return this.path + this.extension;
  }

  get name(): string {
    return super.name + this.extension;
  }

  /**
   * 文件扩展名
   *
   * @author chitanda
   * @date 2021-11-15 19:11:09
   * @type {string}
   */
  readonly extension: string;

  constructor(public readonly content: ISystemFile, extension?: string) {
    super(content, FileType.File);
    if (extension) {
      this.extension = extension;
    } else if (content.extension) {
      this.extension = content.extension;
    } else {
      this.extension = '';
    }
  }

  getUri(): Uri {
    return Uri.parse(`${GlobalConst.FS_PROTOCOL}${this.fullPath}`);
  }
}
